# -*- coding: utf-8 -*-

import requests
import json
import re
import warnings
warnings.filterwarnings("ignore")
import sys
import time
from bs4 import BeautifulSoup


# def craw_taobaogoods(keyword):

# 	url = 'https://list.tmall.com/search_product.htm'
# 	payload = {'q':keyword+'1111',  #随机加代�?
# 				'type':'p',
# 				'spm':'a220m.1000858.a2227oh.d100',
# 				'from':'.list.pc_1_searchbutton'
# 			}

# 	resp = requests.get(url,params = payload,verify=False)
# 	soup = BeautifulSoup(resp.text,'html.parser')
# 	product = str(soup.select('meta')[3]['content']).split(",")[0].split(' ')
# 	if len(product) != 1:
# 		pass
# 	else:
# 		product = soup.select('em')[0].text.replace('�? ','').replace(' �?','').split(' ')[0:-1]
# 	return product

	
# if __name__ == '__main__':

# 	keyword = '平面设�?�广告易拉宝宣传单单彩页画册制作dm图片排版喷绘海报设�??'
# 	print(craw_taobaogoods(keyword))



def craw_taobaogoods(url):

	'''通过链接拆分标�??'''

	# 抓标�?
	resp = requests.get(url,verify=False)
	soup = BeautifulSoup(resp.text,'html.parser')
	keyword = str(soup.select('title')[0].text).split('-')[0]

	# # 拆分
	url = 'https://list.tmall.com/search_product.htm'
	payload = {'q':keyword+'1111',  #随机加代码?
				'type':'p',
				'spm':'a220m.1000858.a2227oh.d100',
				'from':'.list.pc_1_searchbutton'
			}

	resp = requests.get(url,params = payload,verify=False)
	soup = BeautifulSoup(resp.text,'html.parser')
	product = str(soup.select('meta')[3]['content']).split(",")[0].split(' ')
	
	if len(product) != 1:
		pass
	else:
		product = soup.select('em')[0].text.replace('“ ','').replace(' ”','').split(' ')[0:-1]
	return product

	
if __name__ == '__main__':

	url = 'https://detail.tmall.com/item.htm?id=537228433414&ali_refid=a3_420432_1006:1107445584:N:%E5%A5%B3%E5%8C%85:fcd7a3a7552dc912e2741138e7803b93&ali_trackid=1_fcd7a3a7552dc912e2741138e7803b93&spm=a230r.1.14.1'
	print (craw_taobaogoods(url))